package drs.vagir.calculator;

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Stack;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class CalculatorActivity extends Activity {
	//public BigDecimal zero = BigDecimal.ZERO; 
	//public BigDecimal currentNumber = zero;
	public String zero;
	TextView textOfDisplay;
	public Stack<Object> stackOfOperations;
	public boolean isUserInputingNumber = false; 
	public boolean isLastOperationWasEquals = true; 
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        zero = getString(R.string.CountingInitStringZero);
    	stackOfOperations = new Stack<Object>();
        textOfDisplay = (TextView) findViewById(R.id.Display);
    }
    
    public void onButtonClearClick(View view) {
    	//currentNumber = zero;
    	textOfDisplay.setText(zero);
    	findViewById(R.id.ButtonBackspace).setEnabled(false);
    	findViewById(R.id.ButtonClear).setEnabled(false);
    	findViewById(R.id.ButtonEquals).setEnabled(false);
    	isUserInputingNumber = false;
    }
    public void onButtonDigitClick(View view) {
    	Button button = (Button) view;
    	String PressedDigit = (String) button.getText();
    	//String currentNumberStr = currentNumber.toString();
    	String currentNumberStr = (String) textOfDisplay.getText();
    	if (!isUserInputingNumber || currentNumberStr == zero)
    		currentNumberStr = PressedDigit;
    	else {
    		currentNumberStr = currentNumberStr.concat(PressedDigit);
    	}
    	//currentNumber = new BigDecimal(currentNumberStr, MathContext.UNLIMITED);
    	textOfDisplay.setText(currentNumberStr);
    	findViewById(R.id.ButtonBackspace).setEnabled(true);
    	findViewById(R.id.ButtonClear).setEnabled(true);
    	isUserInputingNumber = true;
    }
    public void onButtonBackspaceClick(View view) {
    	//if ( currentNumber.compareTo(zero) != 0 ) {
    	String currentNumberStr = (String) textOfDisplay.getText();
       	if ( currentNumberStr != zero ) {
    		//String currentNumberStr = (String) textOfDisplay.getText();
    		currentNumberStr = currentNumberStr.substring(0, currentNumberStr.length()-1);
    		if ( currentNumberStr.isEmpty() ) {
    			onButtonClearClick(null);
    		} else {
    			//currentNumber = new BigDecimal(currentNumberStr);
    			textOfDisplay.setText(currentNumberStr);
    		}
    	}
    }
    public void onButtonOperationClick(View v) {
    	onButtonEqualsClick(null);

    	int newOperation = v.getId();
    	stackOfOperations.push(newOperation); 

    	findViewById(R.id.ButtonEquals).setEnabled(true);

    	isLastOperationWasEquals = false;
    }
    public void onButtonEqualsClick(View view) {
    	BigDecimal result = BigDecimal.ZERO;
    	String currentNumberStr = (String) textOfDisplay.getText();
    	BigDecimal currentNumber = new BigDecimal(currentNumberStr, MathContext.UNLIMITED);
		BigDecimal newOperand = currentNumber;
		int lastOperation = 0;

		if ( isLastOperationWasEquals ) {
			result = newOperand;
		} else {
			int size = stackOfOperations.size();
    		lastOperation = ((Number) stackOfOperations.elementAt(size-1)).intValue();
    		BigDecimal lastOperand = (BigDecimal) stackOfOperations.elementAt(size-2);

			switch ( lastOperation ) {
	            case R.id.ButtonPlus: // doStuff
	            	result = lastOperand.add(newOperand);
	                break;
	            case R.id.ButtonMultiply: // doStuff
	            	result = lastOperand.multiply(newOperand);
	                break;
	            case R.id.ButtonMinus: // doStuff
	            	result = lastOperand.subtract(newOperand);
	                break;
	            case R.id.ButtonDivide: // doStuff
	            	if ( newOperand != null && newOperand != BigDecimal.ZERO )
	            		result = lastOperand.divide(newOperand);
	            	else
	            		result = BigDecimal.ZERO;
	                break;
	        }
	    	stackOfOperations.push(newOperand); 
    	}
		
    	stackOfOperations.push(result);
    	if ( view != null ) {
    		int newOperation = lastOperation;
    		stackOfOperations.push(newOperation);
    	}

    	currentNumber = result;
		textOfDisplay.setText(currentNumber.toString());

    	findViewById(R.id.ButtonEquals).setEnabled(false);

    	isLastOperationWasEquals = true;
    	isUserInputingNumber = false;
    }
}